package com.cms.work.service.impl.sys;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.work.core.service.impl.BaseServiceImpl;
import com.cms.work.dao.sys.ISysResourceDao;
import com.cms.work.dao.sys.ISysRoleResourceDao;
import com.cms.work.entity.enums.CommonEnum.ResourceStatusEnum;
import com.cms.work.entity.enums.CommonEnum.ResourceTypeEnum;
import com.cms.work.entity.enums.CommonEnum.role;
import com.cms.work.entity.sys.SysResource;
import com.cms.work.entity.sys.SysRoleResource;
import com.cms.work.entity.sys.vo.ResourceRelationVo;
import com.cms.work.entity.sys.vo.SysRoleResourceVo;
import com.cms.work.service.sys.ISysResourceService;

/**
 * @describe 用户资源模块
 * @author YY
 * @version v1.0
 * @date 2016-11-23 10:34:07
 **/
@Service
public class SysResourceServiceImpl extends BaseServiceImpl<SysResource, Integer> implements ISysResourceService {
	
    @Autowired
    private ISysResourceDao sysResourceDao;
    @Autowired
    private ISysRoleResourceDao sysRoleResourceDao;
	
	@Override
	public void save(SysResource sysResource) {
	    if (sysResource.getId() == null || sysResource.getId() == 0) {
	    	sysResourceDao.insertSelective(sysResource);
	    	//每添加一个资源就关联到超级管理员组
            linkRoleAndResource(sysResource);
        } else {
            sysResourceDao.updateByPrimaryKeySelective(sysResource);
        }
	}
	
	private void linkRoleAndResource(SysResource resource) {
		SysRoleResource roleResource = new SysRoleResource();
		roleResource.setRoleId(role.ADMIN.getValue());
		roleResource.setResourceId(resource.getId());
		sysRoleResourceDao.insertSelective(roleResource);
	}

	@Override
	public List<SysRoleResourceVo> selectRoleResourceByRoleId(int roleId) {
		return sysResourceDao.selectRoleResourceByRoleId(roleId);
	}

	@Override
	public void updateResourceStatus(int resourceId, ResourceStatusEnum status) {
		SysResource resource = new SysResource(resourceId, status.getStatus());
		this.updateSelective(resource);
	}

	@Override
	public List<SysRoleResourceVo> getResourceByUserId(int userId) {
		return sysResourceDao.getResourceByUserId(userId);
	}

	@Override
	public List<ResourceRelationVo> getRootResource() {
		return sysResourceDao.getRootResource();
	}

	private static List<Integer> ids = new ArrayList<Integer>();
	@Override
	public String getRootResourceById(Integer pid) {
		if(pid == null || pid == 0){
			return "0";
		}
		SysResource resource = sysResourceDao.selectByPrimaryKey(pid);
		if(resource.getType() == ResourceTypeEnum.menu.getType()){
			ids.add(resource.getId());
		}
		if(resource.getPid() == null || resource.getPid() == 0){
			String result = "";
			Collections.reverse(ids);
			for (int i = 0; i < ids.size(); i++) {
				if(i == (ids.size()-1)){
					result += ids.get(i);
				}else{
					result += ids.get(i)+",";
				}
			}
			ids.clear();
			return result;
		}
		return getRootResourceById(resource.getPid());
	}
	
	@Override
	public List<ResourceRelationVo> getResourceRelation(Integer pid) {
		return sysResourceDao.getResourceRelation(getRootResourceById(pid));
	}

	@Override
	public Integer getMaxSort() {
		return sysResourceDao.getMaxSort();
	}

}

